package com.yougou.utils;

import com.yougou.data.model.User;

public class UserManager {
    
    private static final String KEY_USER = "key_user";
    private static final String KEY_TOKEN = "key_token";
    private static final String KEY_IS_LOGIN = "key_is_login";
    
    private static User currentUser;
    
    
    public static void login(User user) {
        currentUser = user;
        SPUtils.putObject(KEY_USER, user);
        SPUtils.putString(KEY_TOKEN, user.getToken());
        SPUtils.putBoolean(KEY_IS_LOGIN, true);
    }
    
    
    public static void updateUser(User user) {
        if (user == null) return;
        currentUser = user;
        SPUtils.putObject(KEY_USER, user);
    }
    
    
    public static void logout() {
        currentUser = null;
        SPUtils.remove(KEY_USER);
        SPUtils.remove(KEY_TOKEN);
        SPUtils.putBoolean(KEY_IS_LOGIN, false);
        
        // 注意：数据已按用户ID隔离存储，不需要清空
        // 下次登录其他账号时会自动加载对应用户的数据
    }
    
    
    public static boolean isLogin() {
        return SPUtils.getBoolean(KEY_IS_LOGIN, false);
    }
    
    
    public static User getCurrentUser() {
        if (currentUser == null) {
            currentUser = SPUtils.getObject(KEY_USER, User.class);
        }
        return currentUser;
    }
    
    
    public static String getToken() {
        return SPUtils.getString(KEY_TOKEN, "");
    }
}

